package io.renren.modules.sys.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.renren.common.utils.Result;
import io.renren.modules.sys.entity.QInvestigation;
import io.renren.modules.sys.entity.QQuestion;
import io.renren.modules.sys.service.QInvestigationService;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * (QInvestigation)表控制层
 *
 * @author makejava
 * @since 2024-06-29 15:58:05
 */
@RestController
@RequestMapping("qInvestigation")
public class QInvestigationController {
    /**
     * 服务对象
     */
    @Resource
    private QInvestigationService qInvestigationService;

    /**
     * 分页查询
     *
     * @param qInvestigation 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<QInvestigation>> queryByPage(QInvestigation qInvestigation, PageRequest pageRequest) {
        return ResponseEntity.ok(this.qInvestigationService.queryByPage(qInvestigation, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<QInvestigation> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.qInvestigationService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param qInvestigation 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<QInvestigation> add(QInvestigation qInvestigation) {
        return ResponseEntity.ok(this.qInvestigationService.insert(qInvestigation));
    }

    /**
     * 编辑数据
     *
     * @param qInvestigation 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<QInvestigation> edit(QInvestigation qInvestigation) {
        return ResponseEntity.ok(this.qInvestigationService.update(qInvestigation));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.qInvestigationService.deleteById(id));
    }

    @GetMapping("/getQuestionsByInvestigationId")
    @RequiresPermissions("sys:user:info")
    public Result<List<QQuestion>> getQuestionsByInvestigationId(String id){
        List<QQuestion> questions = new ArrayList<>();
        try {
            questions = this.qInvestigationService.getQuestionsByInvestigationId(id);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return new Result<List<QQuestion>>().ok(questions);
    }

}

